/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const {
  override,
  addWebpackModuleRule,
  addWebpackPlugin,
  addWebpackAlias,
  overrideDevServer,
  watchAll,
} = require('customize-cra');
const ArcoWebpackPlugin = require('@arco-plugins/webpack-react');
const addLessLoader = require('customize-cra-less-loader');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  webpack: override(
    addLessLoader({
      lessLoaderOptions: {
        lessOptions: {},
      },
    }),
    addWebpackModuleRule({
      test: /\.svg$/,
      loader: '@svgr/webpack',
    }),
    addWebpackPlugin(
      new ArcoWebpackPlugin({
        theme: '@arco-themes/react-arco-pro',
        modifyVars: {
          'arcoblue-6': '#4AB7D9',
          'arcoblue-5': '#6DD6F7',
          'arcoblue-7': '#44C5E4',
          'arcoblue-4': '#A7E0F2',
          'arcoblue-3': '#CBECF7',
          'arcoblue-2': '#DCF3FA',
          'arcoblue-1': '#EEF9FC',
        },
      })
    ),
    addWebpackAlias({
      '@': path.resolve(__dirname, 'src'),
    }),
    (config) => {
      if (process.env.NODE_ENV === 'production') {
        config.optimization.minimizer.push(
          new TerserPlugin({
            terserOptions: {
              compress: {
                drop_console: true, // 删除所有console.*相关的代码
                pure_funcs: ['console.log'], // 禁用console.log
              },
            },
          })
        );
      }

      // 添加 Hashed Module Ids 配置
      config.optimization = {
        ...config.optimization,
        moduleIds: 'hashed',
      };

      return config;
    }
  ),

  devServer: overrideDevServer((config) => {
    return {
      ...config,
      proxy: {
        '/api': {
          target: 'https://wajib-admin.adaa-test.datepalm.site/',
          changeOrigin: true,
          secure: false,
          pathRewrite: {
            '^/api': '',
          },
        },
      },
    };
  }, watchAll()),
};
